package com.ruoyi.common.utils.email;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.internet.MimeMessage;
import javax.mail.MessagingException;
import java.util.Properties;
import java.io.File;

public class EmailUtil {

    private static final String SMTP_HOST = "smtp.163.com";  // 网易邮箱的 SMTP 主机
    private static final int SMTP_PORT = 465;  // 网易邮箱的 SMTP 端口（465 为 SSL 端口，587 为 STARTTLS 端口）
    private static final String USERNAME = "m19065300975@163.com";  // 你的网易邮箱
    private static final String PASSWORD = "GQaPXkFjN3KsgVVE";  // 你的邮箱密码或授权码

    /**
     * 创建邮件发送对象
     *
     * @return JavaMailSender
     */
    public static JavaMailSender createMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(SMTP_HOST);
        mailSender.setPort(SMTP_PORT);
        mailSender.setUsername(USERNAME);
        mailSender.setPassword(PASSWORD);

        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.smtp.auth", "true");  // 启用SMTP认证
        props.put("mail.smtp.starttls.enable", "true");  // 启用 STARTTLS
        props.put("mail.smtp.ssl.enable", "true");  // 启用 SSL
        props.put("mail.debug", "true");  // 开启调试模式，输出日志

        return mailSender;
    }

    /**
     * 发送简单文本邮件
     *
     * @param to 目标邮箱
     * @param subject 邮件主题
     * @param text 邮件正文
     */
    public static void sendSimpleMail(String to, String subject, String text) {
        JavaMailSender mailSender = createMailSender();
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");  // 设置编码为 UTF-8
            helper.setFrom(USERNAME);  // 发件人
            helper.setTo(to);  // 收件人
            helper.setSubject(subject);  // 主题
            helper.setText(text, true);  // 正文内容，true 表示支持 HTML 格式
            mailSender.send(message);
            System.out.println("邮件发送成功");
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out.println("邮件发送失败");
        }
    }

    /**
     * 发送带附件的邮件
     *
     * @param to 目标邮箱
     * @param subject 邮件主题
     * @param text 邮件正文
     * @param attachmentPath 附件文件路径
     */
    public static void sendMailWithAttachment(String to, String subject, String text, String attachmentPath) {
        JavaMailSender mailSender = createMailSender();
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");  // 设置编码为 UTF-8
            helper.setFrom(USERNAME);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, true);  // 支持 HTML 内容

            // 添加附件
            helper.addAttachment("attachment.txt", new File(attachmentPath));
            mailSender.send(message);
            System.out.println("邮件发送成功，包含附件");
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out.println("邮件发送失败");
        }
    }

    /**
     * 发送 HTML 格式的邮件
     *
     * @param to 目标邮箱
     * @param subject 邮件主题
     * @param htmlContent 邮件正文，HTML 格式
     */
    public static void sendHtmlMail(String to, String subject, String htmlContent) {
        JavaMailSender mailSender = createMailSender();
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");  // 设置编码为 UTF-8
            helper.setFrom(USERNAME);  // 发件人
            helper.setTo(to);  // 收件人
            helper.setSubject(subject);  // 主题
            helper.setText(htmlContent, true);  // 正文内容，true 表示支持 HTML 格式
            mailSender.send(message);
            System.out.println("HTML 邮件发送成功");
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out.println("HTML 邮件发送失败");
        }
    }

}
